﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.DataControl;
using System.Reflection;
using SoiamSys.Model;
using SoiamSys.Common;

namespace System.Data.DataModel
{
    public class BaseModel
    {
        protected IDbController dbController;

        private string message;

        


        #region constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dataProvider"></param>
        public BaseModel()
        {
            this.dbController = DbController.DbControllerFactory();
            this.dbController.Command.SetBindByName(true);
        }


        #endregion

        /// <summary>
        /// Get All Data from Table
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<object> GetAll()
        {
            return this.dbController.GetAll(this);
        }


        /// <summary>
        /// Set Data to Model object
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public virtual bool GetData(string[] primaryKey)
        {
            try
            {
                this.dbController.GetData(this, primaryKey).First(delegate(object obj)
                {
                    Type type = obj.GetType();
                    foreach (PropertyInfo property in type.GetProperties())
                    {
                        property.SetValue(this, property.GetValue(obj, null), null);
                    }
                    return true;
                });
                return true;
            }
            catch (Exception e)
            {
                this.message = e.Message;
                return false;
            }


        }

        /// <summary>
        /// Get All Data from Table
        /// </summary>
        /// <returns></returns>
        public virtual IEnumerable<object> GetData()
        {
            return this.dbController.GetData(this);
        }

        /// <summary>
        /// Update Model Data
        /// </summary>
        /// <returns></returns>
        public virtual int Update()
        {
            //パラメータを文字列にする
            this.SetParameterString();
            return this.dbController.Update(this);
        }

        /// <summary>
        /// Insert Model Data
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public virtual int Insert(string[] primaryKey)
        {
            //パラメータを文字列にする
            this.SetParameterString();
            return this.dbController.Insert(this, primaryKey);
        }

        /// <summary>
        /// Delete Model Data
        /// </summary>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public virtual int Delete(string primaryKey)
        {
            //パラメータを文字列にする
            this.SetParameterString();
            return this.dbController.Delete(this, primaryKey);
        }

        public void SetTransaction()
        {
            this.dbController.SetTransaction();
        }

        //パラメーターの設定
        private string parameterString;

        private void SetParameterString()
        {
            StringBuilder builder = new StringBuilder();
            foreach (System.Data.IDataParameter parameter in this.dbController.Command.Parameters)
            {
                builder.Append(parameter.ParameterName + "=" + parameter.Value + ",");
            }
            this.parameterString = builder.ToString();
           
            
        }

        /// <summary>
        /// C-002_操作履歴保存機能
        /// </summary>
        /// <param name="personCode"></param>
        /// <param name="functionID"></param>
        /// <param name="functionName"></param>
        /// <param name="className"></param>
        /// <param name="orderID"></param>
        /// <param name="message"></param>
        public void InsertHistory(string personCode, string functionID, 
                                    string className, string orderID, string message)
        {
            //操作履歴保存
            //担当者名を取得
            PersonMaster person = new PersonMaster();
            person.PersonCode = personCode;
            string[] parameter = {"PersonCode"};
            person.GetData(parameter);
            OperationHistory operationHistory = new OperationHistory();
            operationHistory.PersonCode = personCode;
            operationHistory.PersonName = person.PersonName;
            operationHistory.FunctionID = functionID;
            //機能名は機能IDから取得する
            FunctionCollection functions = new FunctionCollection();
            operationHistory.FunctionName = functions.GetFunctionName(functionID);
            operationHistory.ClassName = className;
            operationHistory.OrderID = orderID;
            operationHistory.Message = message;
            string[] insertParameter = { "SequenceNumber" };
            operationHistory.Insert(insertParameter);
        }
 
    }
}
